package com.ccl.iot.template;import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.ccl.iot.R;

public class DialogEditable extends DialogTitled {
	public interface OnDoneListener{
		public void onDone(DialogInterface aDialog);
	}
	
	/*
	 * 	Non-static functions
	 */
	private ImageButtonView iDone = null;
	
	private OnDoneListener iOnDoneListener = null;
	
	public DialogEditable(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		iDone = new ImageButtonView(this.getContext());
		iDone.setImageResource(R.drawable.icon_save);
		super.addRightTitleView(iDone);
		
		handleButtonEvents();
	}
	
	@Override
	public void addRightTitleView(View aView) {
		int tCount = super.getRightTitleViewCount();
		
		if (tCount == 1){
			super.addRightTitleView(aView, 0);
		}else{
			super.addRightTitleView(aView, tCount - 1);
		}
	}
	
	@Override
	public void removeRightTitleView(int aIndex){
		removeRightTitleView(aIndex);
	}
	
	@Override
	public void removeAllRightTitleView(){
		for (int i = 0, tCount = super.getRightTitleViewCount() - 1; i < tCount; i++){
			super.removeRightTitleView(0);
		}
	}
	
	public void setOnDoneListener(OnDoneListener aListener){
		iOnDoneListener = aListener;
	}
	
	protected boolean onDone(){
		return true;
	}
	
	private void handleButtonEvents(){
		if (iDone != null){
			iDone.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (onDone()){
						if (iOnDoneListener != null){
							iOnDoneListener.onDone(DialogEditable.this);
						}
						
						dismiss();
					}
				}
			});
		}
	}
}
